#ifndef FILESERVER_H
#define FILESERVER_H

#include <QTime>
#include <QMainWindow>

QT_BEGIN_NAMESPACE
class QTcpSocket;
class QTcpServer;
class QUdpSocket;
class QTreeWidget;
class QTime;
class QTimerEvent;
class QPushButton;
class QFile;
class QWidget;
class QSplitter;
class GeneralTab;
class logTab;
class QLabel;
class QTreeWidgetItem;
QT_END_NAMESPACE

const int PORT = 50000;
const int BROADCASTPORT = 50001;
const int UDPPORT = 50002;
const int BUFFERSIZE = 108544;
const char logFileName[] = "log";
const char dataDir[] = "data/";

class FileServer: public QMainWindow
{
    Q_OBJECT

public:
    FileServer(QWidget *parent = 0);

public:
    void newTcpClient();
    int clientAtRow(QTcpSocket *client);
    int progress(int row);

private slots:
    void processUdpData();
    void newTcpConnect();
    void processTcpData();
    void closeAllTcpConnect();
    void jobTerminate();
    void startServer();
    //add methos by wayne
    void tabWidgetOn(QTreeWidgetItem *clickedItem);
    void tabWidgetOff();
    void checkLog();
    void selectionChanged();
    void closeAll();

protected:
    void timerEvent(QTimerEvent *);

private:
    void startUdpServer();
    void startTcpServer();
    //add methos by wayne
    void createActions();
    void createToolbars();
    void createSplitter();
    void createStatusBar();
    void testTreeWidget();

private:
    QTcpServer *tcpserver;
    QUdpSocket *udpserver;

    QTreeWidget *filesview;
    //QPushButton *okButton;
    //QPushButton *cancelButton;
    //QPushButton *quitButton;

    struct Job
    {
        QTcpSocket *client;
        int size;
        int leftsize;
        QFile *file;
        QString filename;
        int sendCount;
        int lastRecvId;
        unsigned int sendBlock;
        int32_t nextBlockSize;
        QString strsize;
        QTime startTime;
        int timerID;
    };
    QList<Job *> Jobs;

    //add some variables by wayne
    QMenu *fileMenu;
    QMenu *actionMenu;
    QMenu *helpMenu;
    QToolBar *topBar;
    QAction *m_connect;
    QAction *m_disallconnect;
    QAction *m_quit;
    QAction *m_checkLog;
    QAction *m_about;
    QSplitter *splitter;
    QTabWidget *tabWidget;
    GeneralTab *gTab;
    logTab *lTab;
    QLabel *tipLabel;
};

#endif // FILESERVER_H
